package com.shinhye.elephantsafe.db.dao;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class AddressDao {
	private static String path = "/data/data/com.shinhye.elephantsafe/files/address.db";

	public static String addressQuery(String number) {
		SQLiteDatabase db = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
		
		String address = null;
		String regex = "^1[3458]\\d{9}$";
		if(number.matches(regex)) {
			String subNumber = number.substring(0, 7);
			Cursor cursor = db.rawQuery("select location from data2 where id=(select outkey from data1 where id=?)", new String[]{subNumber});
			if(cursor.moveToNext())
				address = cursor.getString(0);
			
			cursor.close();
			db.close();
			return address;
		}
		
		switch(number.length()) {
		case 3:
			if("110".equals(number))
				address = "报警电话";
			else if("119".equals(number))
				address = "火警电话";
			else if("120".equals(number))
				address = "急救电话";
			else if("999".equals(number))
				address = "香港急救电话";
			else if("911".equals(number))
				address = "美国报警电话";
			break;
		case 4:
			address = "模拟器号码";
			break;
		case 5:
			address = "客户服务号码";
			break;
		case 7:
		case 8:
			address = "本地号码";
			break;
		default:
			if(number.length() >= 10 && number.length() <= 12 && number.startsWith("0")) {
				//010 - 2312211
				String subNumber = number.substring(1, 3);
				Cursor cursor = db.rawQuery("select location from data2 where area=?", new String[]{subNumber});
				if(cursor.moveToNext()) {
					address = cursor.getString(0);
					address = address.substring(0, address.length() - 2);
				}
				
				//0551 - 32149712
				subNumber = number.substring(1, 4);
				cursor = db.rawQuery("select location from data2 where area=?", new String[]{subNumber});
				if(cursor.moveToNext()) {
					address = cursor.getString(0);
					address = address.substring(0, address.length() - 2);
				}
				cursor.close();
			}
		}
		
		db.close();
		return address;
	}
}
